//
//  View_YHExtension.swift
//  poetry
//
//  Created by YL-Hong on 2022/3/8.
//

import Foundation
import UIKit


//MARK: -----------------------Frame 扩展----------------------------
extension UIView {
    /// x 的位置
      var yh_x: CGFloat {
          get {
              return self.frame.origin.x
          }
          set(newValue) {
              var tempFrame: CGRect = self.frame
              tempFrame.origin.x = newValue
              self.frame = tempFrame
          }
      }
    
    /// y 的位置
       var yh_y: CGFloat {
           get {
               return self.frame.origin.y
           }
           set(newValue) {
               var tempFrame: CGRect = self.frame
               tempFrame.origin.y = newValue
               self.frame = tempFrame
           }
       }
    
    ///height : 视图高度
    var yh_height: CGFloat {
        get {
            return self.frame.size.height
        }
        set(newValue) {
            var tempFrame: CGRect = self.frame
            tempFrame.size.height = newValue
            self.frame = tempFrame
        }
    }
    
    /// width: 视图的宽度
       var yh_width: CGFloat {
           get {
               return self.frame.size.width
           }
           set(newValue) {
               var tempFrame: CGRect = self.frame
               tempFrame.size.width = newValue
               self.frame = tempFrame
           }
       }
    
    /// size: 视图的zize
      var yh_size: CGSize {
          get {
              return self.frame.size
          }
          set(newValue) {
              var tempFrame: CGRect = self.frame
              tempFrame.size = newValue
              self.frame = tempFrame
          }
      }
    
    /// centerX: 视图的X中间位置
       var yh_centerX: CGFloat {
           get {
               return self.center.x
           }
           set(newValue) {
               var tempCenter: CGPoint = self.center
               tempCenter.x = newValue
               self.center = tempCenter
           }
       }
    
    /// centerY: 视图Y的中间位置
       var yh_centerY: CGFloat {
           get {
               return self.center.y
           }
           set(newValue) {
               var tempCenter: CGPoint = self.center
               tempCenter.y = newValue
               self.center = tempCenter;
           }
       }
    
    /// bottom 底端纵坐标 (y + height)
      var yh_bottom: CGFloat {
          get {
              return self.frame.origin.y + self.frame.size.height
          }
          set(newValue) {
              self.frame.origin.y = newValue - self.frame.size.height
          }
      }

    /// right 底端纵坐标 (x + width)
      var yh_right: CGFloat {
          get {
              return self.frame.origin.x + self.frame.size.width
          }
          set(newValue) {
              self.frame.origin.x = newValue - self.frame.size.width
          }
      }
      
   /// 圆角
    var yh_cornerRadius: CGFloat {
        get {
           return  self.layer.cornerRadius
        }
        set {
            self.layer.cornerRadius = newValue
            self.layer.masksToBounds = true
        }
    }
    
    
}



extension UIView {
    
 /**移除所有子视图*/
 func removeAllSubview(){
     
     for v in self.subviews {
         v.removeFromSuperview()
     }
 }
  
    
    
  var getCurrentVC: UIViewController? {
      
      for view in sequence(first: self.superview, next: { $0?.superview }) {

              if let responder = view?.next {

                  if responder.isKind(of: UIViewController.self){

                      return responder as? UIViewController

                  }
              }
          }
          return nil
   }
    
    //MARK: 添加指定圆角
    ///   - conrners: 具体哪个圆角
    ///   - radius: 圆角的大小
    func addCorner(conrners: UIRectCorner = [.topLeft,.topRight,.bottomLeft,.bottomRight] , radius: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
    }
    

    // MARK: 给继承于view的类添加阴影
       /// - Parameters:
       ///   - shadowColor: 阴影的颜色
       ///   - shadowOffset: 阴影的偏移度：CGSizeMake(X[正的右偏移,负的左偏移], Y[正的下偏移,负的上偏移]);
       ///   - shadowOpacity: 阴影的透明度
       ///   - shadowRadius: 阴影半径，默认 3
       func addShadow(shadowColor: UIColor, shadowOffset: CGSize, shadowOpacity: Float, shadowRadius: CGFloat = 3) {
           // 设置阴影颜色
           layer.shadowColor = shadowColor.cgColor
           // 设置透明度
           layer.shadowOpacity = shadowOpacity
           // 设置阴影半径
           layer.shadowRadius = shadowRadius
           // 设置阴影偏移量
           layer.shadowOffset = shadowOffset
       }
    
    
    // MARK: 添加阴影和圆角并存
       /// 添加阴影和圆角并存
       /// - Parameters:
       ///   - conrners: 具体哪个圆角
       ///   - radius: 圆角大小
       ///   - shadowColor: 阴影的颜色
       ///   - shadowOffset: 阴影的偏移度：CGSizeMake(X[正的右偏移,负的左偏移], Y[正的下偏移,负的上偏移]);
       ///   - shadowOpacity: 阴影的透明度
       ///   - shadowRadius: 阴影半径，默认 3
       func addCornerAndShadow(superview: UIView, conrners: UIRectCorner , radius: CGFloat = 3, shadowColor: UIColor, shadowOffset: CGSize, shadowOpacity: Float, shadowRadius: CGFloat = 3) {
       
           let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
           let maskLayer = CAShapeLayer()
           maskLayer.frame = self.bounds
           maskLayer.path = maskPath.cgPath
           self.layer.mask = maskLayer
       
           let subLayer = CALayer()
           let fixframe = self.frame
           subLayer.frame = fixframe
           subLayer.cornerRadius = shadowRadius
           subLayer.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.3).cgColor
           subLayer.masksToBounds = false
           // shadowColor阴影颜色
           subLayer.shadowColor = shadowColor.cgColor
           // shadowOffset阴影偏移,x向右偏移3，y向下偏移2，默认(0, -3),这个跟shadowRadius配合使用
           subLayer.shadowOffset = shadowOffset
           // 阴影透明度，默认0
           subLayer.shadowOpacity = shadowOpacity
           // 阴影半径，默认3
           subLayer.shadowRadius = shadowRadius
           superview.layer.insertSublayer(subLayer, below: self.layer)
       }

    ///设置渐变色
    ///cgcolors[CGColor]: 渐变色数组
    ///左上角位置的坐标是 (0, 0)，右下角位置的坐标是 (1, 1)
    ///默认startPoint(0.5,0.0) endpoint(0.5,1.0)  垂直向下渐变
    ///locations 控制颜色位置0-1之间
    func setGradientColor(cgcolors: [CGColor],startPoint: CGPoint = CGPoint(x: 0.5, y: 0.0), endPoint: CGPoint = CGPoint(x: 0.5, y: 1.0),locations:[NSNumber] = [0.0,1.0]){
        let gradient = CAGradientLayer()
        gradient.frame = self.bounds
        gradient.colors = cgcolors
        gradient.startPoint = startPoint
        gradient.endPoint = endPoint
        gradient.locations = locations
        self.layer.addSublayer(gradient)
    }

    
}
